// 导入Vue
import Vue from 'vue';
// 导入vue-router
import VueRouter from 'vue-router';
// 将vue-router注册成Vue的插件
Vue.use(VueRouter);


// 导入路由组件
import Login from './pages/Login';
import Layout from './Layout';
// 导入Home组件
import Home from './pages/Home'
// 导入菜单组件
import MenuList from './pages/menu/List';
import MenuAdd from './pages/menu/Add';
import MenuEdit from './pages/menu/Edit';
// 角色管理组件
import RoleList from './pages/role/List';
import RoleEdit from './pages/role/Edit';
import RoleAdd from './pages/role/Add';
// 管理员组件
import Admin from './pages/admin/List';
import AdminAdd from './pages/admin/Add';
import AdminEdit from './pages/admin/Edit';
// 分类管理
import Category from './pages/category/List';
import CategoryAdd from './pages/category/Add';
import CategoryEdit from './pages/category/Edit';

// 商品管理
import GoodsAdd from './pages/goods/Add';

import { Message } from 'element-ui';

// 1-定义路由规则表
const routes=[
    {
        path:'/login',
        component:Login
    },
    {
        path:'/',
        component:Layout,
        redirect:'/home',
        children:[
            {
                path:'/home',
                component:Home
            },
            {
                path:'/menu',
                component:MenuList
            },
            {
                path:'/menu/add',
                component:MenuAdd
            },
            {
                path:'/menu/:id',
                component:MenuEdit
            },
            {
                path:'/role',
                component:RoleList
            },
            {
                path:'/role/add',
                component:RoleAdd
            },
            {
                path:'/role/:id',
                component:RoleEdit
            },
            {
                path:'/admin',
                component:Admin
            },
            {
                path:'/admin/add',
                component:AdminAdd
            },
            {
                path:'/admin/edit',
                component:AdminEdit
            },
            {
                path:'/category',
                component:Category
            },
            {
                path:'/category/add',
                component:CategoryAdd
            },
            {
                path:'/category/:id',
                component:CategoryEdit
            },
            {
                path:'/goods/Add',
                component:GoodsAdd
            }
        ]
    }
];

// 2-创建路由对象
const router=new VueRouter({
    routes
});


// 路由守卫
router.beforeEach((to,from,next)=>{
    if(to.path!='/login'){
        // 进行登录验证
        let userInfo=sessionStorage.getItem('userinfo');
        if(!userInfo){
            Message({
                type:'error',
                message:'请先登录!'
            })
            // 重定向到登录页面
           return next('/login');
        }
        // 判断是否有访问权限
        userInfo=JSON.parse(userInfo);
        // 获取当前页面路由
        const pathArr=to.path.split('/');
        userInfo.menus_url=['/','/home',...userInfo.menus_url];
        if(userInfo.menus_url.indexOf(`/${pathArr[1]}`)==-1){
            Message({
                type:'error',
                message:'暂无访问权限!'
            })
            return next('/');
        }
        return next();
    }
    next();
});

// 3-导出路由实例对象
export default router;